import { defineStore } from "pinia";
import { getLinksListApi, searchLinks } from "@/service/links/links";
import type { Link } from "@/service/links/type";
import Swal from "sweetalert2";

export const useLinkStore = defineStore("link", {
  state: () => ({
    linkList: [] as Link[],
    total: 0,
    totalPages: 0,
    searchList: [] as Link[],
  }),
  actions: {
    async getLinksList(page?: number, pageSize?: number) {
      try {
        const res = await getLinksListApi(page, pageSize);
        const { list, total, totalPages } = res.data;
        this.linkList = list;
        this.total = total;
        this.totalPages = totalPages;
        return res.data;
      } catch (error) {
        Swal.fire({
          title: "获取链接列表失败",
          icon: "error",
        });
      }
    },
    setSearchResults(results: any[]) {
      this.searchList = results;
    },
  },
});
